/*
 * PublicSnmpCommon.cpp
 *
 *  Created on: 2017-4-12
 *      Author: root
 */
//0找个匹配关键字将关键字替换为需要的关键子
#include"PublicTool.h"
#include<stdio.h>
#include<stdlib.h>
#include"PublicSnmpCommon.h"
//__asm__(".symver __isoc99_sscanf,sscanf@GLIBC_2.2.5");
//将指定描述简化
int CheckifDescr(char*ifDescr,const char*KeyWord,const char*ReplaceWord){
    if(strncmp(ifDescr,KeyWord,strlen(KeyWord))==0){
		char temp[64]={0};
		sprintf(temp,"%s%s",ReplaceWord,ifDescr+strlen(KeyWord));
		strcpy(ifDescr,temp);
		return 0;
    }
    return -1;
}
typedef struct _ifDescrCheck{
const char*startstr;
const char*simplestr;
}ifDescrCheck;
static ifDescrCheck g_ifDescrCheck[]={
	{"Register-Tunnel","RT"},
	{"Stack-Port","SP"},
	{"StackSub-St","SS"},
	{"Null","NULL"},
	{"Loopback","LoopBack"},
	{"InLoopBack","InLoopBack"},
	{"IPInterface","Interface"},
	{"Vlan-interface","Vlan"},
	{"Vlanif","Vlan"},
	{"VLAN","Vlan"},
	{"AggregatePort","AP"},
	{"M-Ethernet","ME"},
	{"MEth","ME"},
	{"M-GigabitEthernet","ME"},
	{"Mgmt","ME"},
	{"FastEthernet","FE"},
	{"Ethernet","E"},
	{"FortyGigabitEthernet","FGE"},
	{"FortyGigE","FGE"},
	{"40GE","FGE"},
	{"10GE","TGE"},
	{"TenGigabitEthernet","TGE"},
	{"Ten-GigabitEthernet","TGE"},
	{"XGigabitEthernet","TGE"},
	{"GigabitEthernet","GE"},
	{"Port-channel","PC"},
	{"Bridge-Aggregation","BA"},
	{"Port #","Port"},
	{"Link Aggregate ","LA"},
	{"Eth-Trunk","ET"},
	{NULL,NULL}
};
void SimpleifDescr(char*ifDescr){
	int i=0;
	while(g_ifDescrCheck[i].startstr!=NULL){
		if(CheckifDescr(ifDescr,g_ifDescrCheck[i].startstr,g_ifDescrCheck[i].simplestr)==0){
			break;
		}
		i++;
	}
//其它特殊采集方式
	if(g_ifDescrCheck[i].startstr==NULL){
		if(strcasestr(ifDescr,"Gigabit - Level")!=NULL){
			int unit,slot,port;
			char temp[64]={0};
			if(sscanf(ifDescr,"Unit: %d Slot: %d Port: %d",&unit,&slot,&port)==3){
				sprintf(temp,"GE%d/%d/%d",unit,slot,port);
				strcpy(ifDescr,temp);
			}
		}else
		if(strcasestr(ifDescr,"10G - Level")!=NULL){
			int unit,slot,port;
			char temp[64]={0};
			if(sscanf(ifDescr,"Unit: %d Slot: %d Port: %d",&unit,&slot,&port)==3){
				sprintf(temp,"TGE%d/%d/%d",unit,slot,port);
				strcpy(ifDescr,temp);
			}
		}else
		if(strcasestr(ifDescr,"CPU Interface for ")!=NULL){
			int unit,slot,port;
			char temp[64]={0};
			if(sscanf(ifDescr+strlen("CPU Interface for "),"Unit: %d Slot: %d Port: %d",&unit,&slot,&port)==3){
			   sprintf(temp,"Interface%d/%d/%d",unit,slot,port);
			   strcpy(ifDescr,temp);
			}
		}
	}
    size_t len=strlen(ifDescr);
    TRIMC(ifDescr,&len);
}

//获取关键字符串
//开始字符，结束字符，开头是否过滤，返回下一个位置
const char*GetKeyStr(const char*buffer,const char*start,const char*end,int bNeedStart,char*result,size_t len=64){
    if(buffer==NULL)return NULL;
    const char*head=buffer;
    if(start!=NULL)head=strstr(buffer,start);
    if(head==NULL)return NULL;
    if(bNeedStart==0){
		if(start!=NULL)
			head=head+strlen(start);
    }
    if(end[0]!='\0'){
		const char*pos=strstr(head,end);
		if(pos){
			size_t tmplen=pos-head;
			if(tmplen>len)tmplen=len;
			memcpy(result,head,tmplen);
			return pos+strlen(end);
        }
    }else{
		size_t tmplen=strlen(head);
		if(tmplen>len)tmplen=len;
		memcpy(result,head,tmplen);
		return NULL;
    }
    return NULL;
}

void ParseSysDescr(const char*sysDescr,int ManufacturerID,char*softVersion,char*ProductName,char*Manufacturer){
if(ManufacturerID==PRODUCT_NULL){
	strcpy(Manufacturer,"NULL");
}else
if(ManufacturerID==PRODUCT_CISCO){
	strcpy(Manufacturer,"Cisco");
/*
Cisco IOS Software, C3560E Software (C3560E-UNIVERSALK9-M), Version 12.2(50)SE2, RELEASE SOFTWARE (fc2)
Copyright (c) 1986-2009 by Cisco Systems, Inc.
Compiled Fri 15-May-09 22:11 by nachen
 */
/*
Cisco Internetwork Operating System Software
IOS (tm) C2950 Software (C2950-I6Q4L2-M), Version 12.1(22)EA7, RELEASE SOFTWARE (fc1)
Copyright (c) 1986-2006 by cisco Systems, Inc.
*/
//先查找IOS关键字，再查找Software关键字
	const char*Pos=sysDescr;
	Pos=strstr(Pos,"IOS");
	if(Pos)Pos=strstr(Pos,"Software");
	if(Pos)Pos=GetKeyStr(Pos,"(",")",0,ProductName);
	if(Pos)GetKeyStr(Pos,"Version ","\r\n",1,softVersion);
}else
if(ManufacturerID==PRODUCT_HUAWEI){
	strcpy(Manufacturer,"Huawei");
/*
Huawei Versatile Routing Platform Software
VRP (R) software, Version 8.100 (CE6851HI V100R005C10SPC200)
Copyright (C) 2012-2015 Huawei Technologies Co., Ltd.
HUAWEI CE6851-48S6Q-HI
*/
/*
Huawei Versatile Routing Platform Software 
VRP (R) software, Version 8.150 (CE6851HI V200R002C50SPC800) 
Copyright (C) 2012-2017 Huawei Technologies Co., Ltd. 
HUAWEICE6851-48S6Q-HI 
*/
	if(strncmp(sysDescr,"Huawei",6)==0){
		const char*Pos=sysDescr;
		Pos=GetKeyStr(Pos,"Version","\r\n",1,softVersion);
		if(Pos){
			GetKeyStr(Pos,"HUAWEI ","\r\n",0,ProductName);
			if(ProductName[0]=='\0'){
				GetKeyStr(Pos,"HUAWEI","\r\n",0,ProductName);
			}
		}
	}else
/*
HUAWEI-3COM CORP. SNMP agent for QuidWay S6500
*/
	if(strncmp(sysDescr,"HUAWEI-3COM",11)==0){
		const char*Pos=sysDescr;
		Pos=GetKeyStr(Pos,"for ","",0,ProductName);
		strcpy(softVersion,ProductName);
	}else{
/*
S5700-28C-SI
Huawei Versatile Routing Platform Software
VRP (R) software,Version 5.70 (S5700 V100R005C01SPC100)
Copyright (C) 2000-2006 Huawei Technologies Co., Ltd.
*/
		const char*Pos=sysDescr;
		Pos=GetKeyStr(Pos,NULL,"\r\n",0,ProductName);
		if(Pos)GetKeyStr(Pos,"Version","\r\n",1,softVersion);
    }
//去掉前面Quidway字样
	if(strncmp(ProductName,"Quidway ",8)==0){
		char tmp[64]={0};
		strcpy(tmp,ProductName);
		strcpy(ProductName,tmp+8);
    }
}else
if(ManufacturerID==PRODUCT_H3C){
	strcpy(Manufacturer,"H3C");
/*
H3C Comware Platform Software, Software Version 7.1.045, Release 3106
H3C S5130-28S-EI
Copyright (c) 2004-2014 Hangzhou H3C Tech. Co., Ltd. All rights reserved.
*/
/*
H3C Comware Platform Software
Comware software, Version 3.10, Release 1602P01
S5600-26C Product Version S5600-1602P01

Copyright(c) 2004-2008 Hangzhou H3C Tech. Co.,Ltd. All rights reserved.
 */
	if(strncmp(sysDescr,"H3C Comware Platform Software",29)==0){
		const char*Pos=sysDescr;
		Pos=GetKeyStr(Pos,"Version","\r\n",1,softVersion);
		if(Pos){
			if(strncmp(Pos,"H3C ",4)==0){
				GetKeyStr(Pos,"H3C ","\r\n",0,ProductName);
			}else{
				GetKeyStr(Pos,NULL," Product",0,ProductName);
            }
        }
	}else{
		const char*Pos=sysDescr;
		Pos=GetKeyStr(Pos,"H3C "," Product",0,ProductName);
		if(Pos)GetKeyStr(Pos,"Version","",1,softVersion);
    }
/*
H3C S5024P Product Version - S5024P-R0109
*/
}else
if(ManufacturerID==PRODUCT_DELL){
	strcpy(Manufacturer,"Dell");
}else
if(ManufacturerID==PRODUCT_RUIJIE){
/*
Ruijie 10G DataCenter Switch(S6000C-48GT4XS-E) By Ruijie Networks
*/
	strcpy(Manufacturer,"Ruijie");
	GetKeyStr(sysDescr,"(",")",0,ProductName);
}else
if(ManufacturerID==PRODUCT_WINDOWS){
	strcpy(Manufacturer,"Windows");
}else
if(ManufacturerID==PRODUCT_UNIX){
	strcpy(Manufacturer,"Unix");
}else{
	strcpy(Manufacturer,"未知");
}
}
typedef struct _ifTypeInfo{
int ifType;
char Desrc[64];
}ifTypeInfo;
static ifTypeInfo ifTypeInfoList[]={
{1,"other"},
{2,"regular1822"},
{3,"hdh1822"},
{4,"ddnX25"},
{5,"rfc877x25"},
{6,"ethernetCsmacd"},
{7,"iso88023Csmacd"},
{8,"iso88024TokenBus"},
{9,"iso88025TokenRing"},
{10,"iso88026Man"},
{11,"starLan"},
{12,"proteon10Mbit"},
{13,"proteon80Mbit"},
{14,"hyperchannel"},
{15,"fddi"},
{16,"lapb"},
{17,"sdlc"},
{18,"ds1"},
{19,"e1"},
{20,"basicISDN"},
{21,"primaryISDN"},
{22,"propPointToPointSerial"},
{23,"ppp"},
{24,"softwareLoopback"},
{25,"eon"},
{26,"ethernet3Mbit"},
{27,"nsip"},
{28,"slip"},
{29,"ultra"},
{30,"ds3"},
{31,"sip"},
{32,"frameRelay"},
{33,"rs232"},
{34,"para"},
{35,"arcnet"},
{36,"arcnetPlus"},
{37,"atm"},
{38,"miox25"},
{39,"sonet"},
{40,"x25ple"},
{41,"iso88022llc"},
{42,"localTalk"},
{43,"smdsDxi"},
{44,"frameRelayService"},
{45,"v35"},
{46,"hssi"},
{47,"hippi"},
{48,"modem"},
{49,"aal5"},
{50,"sonetPath"},
{51,"sonetVT"},
{52,"smdsIcip"},
{53,"propVirtual"},
{54,"propMultiplexor"},
{55,"ieee80212"},
{56,"fibreChannel"},
{57,"hippiInterface"},
{58,"frameRelayInterconnect"},
{59,"aflane8023"},
{60,"aflane8025"},
{61,"cctEmul"},
{62,"fastEther"},
{63,"isdn"},
{64,"v11"},
{65,"v36"},
{66,"g703at64k"},
{67,"g703at2mb"},
{68,"qllc"},
{69,"fastEtherFX"},
{70,"channel"},
{71,"ieee80211"},
{72,"ibm370parChan"},
{73,"escon"},
{74,"dlsw"},
{75,"isdns"},
{76,"isdnu"},
{77,"lapd"},
{78,"ipSwitch"},
{79,"rsrb"},
{80,"atmLogical"},
{81,"ds0"},
{82,"ds0Bundle"},
{83,"bsc"},
{84,"async"},
{85,"cnr"},
{86,"iso88025Dtr"},
{87,"eplrs"},
{88,"arap"},
{89,"propCnls"},
{90,"hostPad"},
{91,"termPad"},
{92,"frameRelayMPI"},
{93,"x213"},
{94,"adsl"},
{95,"radsl"},
{96,"sdsl"},
{97,"vdsl"},
{98,"iso88025CRFPInt"},
{99,"myrinet"},
{100,"voiceEM"},
{101,"voiceFXO"},
{102,"voiceFXS"},
{103,"voiceEncap"},
{104,"voiceOverIp"},
{105,"atmDxi"},
{106,"atmFuni"},
{107,"atmIma"},
{108,"pppMultilinkBundle"},
{109,"ipOverCdlc"},
{110,"ipOverClaw"},
{111,"stackToStack"},
{112,"virtualIpAddress"},
{113,"mpc"},
{114,"ipOverAtm"},
{115,"iso88025Fiber"},
{116,"tdlc"},
{117,"gigabitEthernet"},
{118,"hdlc"},
{119,"lapf"},
{120,"v37"},
{121,"x25mlp"},
{122,"x25huntGroup"},
{123,"transpHdlc"},
{124,"interleave"},
{125,"fast"},
{126,"ip"},
{127,"docsCableMaclayer"},
{128,"docsCableDownstream"},
{129,"docsCableUpstream"},
{130,"a12MppSwitch"},
{131,"tunnel"},
{132,"coffee"},
{133,"ces"},
{134,"atmSubInterface"},
{135,"l2vlan"},
{136,"l3ipvlan"},
{137,"l3ipxvlan"},
{138,"digitalPowerline"},
{139,"mediaMailOverIp"},
{140,"dtm"},
{141,"dcn"},
{142,"ipForward"},
{143,"msdsl"},
{144,"ieee1394"},
{145,"if-gsn"},
{146,"dvbRccMacLayer"},
{147,"dvbRccDownstream"},
{148,"dvbRccUpstream"},
{149,"atmVirtual"},
{150,"mplsTunnel"},
{151,"srp"},
{152,"voiceOverAtm"},
{153,"voiceOverFrameRelay"},
{154,"idsl"},
{155,"compositeLink"},
{156,"ss7SigLink"},
{157,"propWirelessP2P"},
{158,"frForward"},
{159,"rfc1483"},
{160,"usb"},
{161,"ieee8023adLag"},
{162,"bgppolicyaccounting"},
{163,"frf16MfrBundle"},
{164,"h323Gatekeeper"},
{165,"h323Proxy"},
{166,"mpls"},
{167,"mfSigLink"},
{168,"hdsl2"},
{169,"shdsl"},
{170,"ds1FDL"},
{171,"pos"},
{172,"dvbAsiIn"},
{173,"dvbAsiOut"},
{174,"plc"},
{175,"nfas"},
{176,"tr008"},
{177,"gr303RDT"},
{178,"gr303IDT"},
{179,"isup"},
{180,"propDocsWirelessMaclayer"},
{181,"propDocsWirelessDownstream"},
{182,"propDocsWirelessUpstream"},
{183,"hiperlan2"},
{184,"propBWAp2Mp"},
{185,"sonetOverheadChannel"},
{186,"digitalWrapperOverheadChannel"},
{187,"aal2"},
{188,"radioMAC"},
{189,"atmRadio"},
{190,"imt"},
{191,"mvl"},
{192,"reachDSL"},
{193,"frDlciEndPt"},
{194,"atmVciEndPt"},
{195,"opticalChannel"},
{196,"opticalTransport"},
{197,"propAtm"},
{198,"voiceOverCable"},
{199,"infiniband"},
{200,"teLink"},
{201,"q2931"},
{202,"virtualTg"},
{203,"sipTg"},
{204,"sipSig"},
{205,"docsCableUpstreamChannel"},
{206,"econet"},
{207,"pon155"},
{208,"pon622"},
{209,"bridge"},
{210,"linegroup"},
{211,"voiceEMFGD"},
{212,"voiceFGDEANA"},
{213,"voiceDID"},
{214,"mpegTransport"},
{215,"sixToFour"},
{216,"gtp"},
{217,"pdnEtherLoop1"},
{218,"pdnEtherLoop2"},
{219,"opticalChannelGroup"},
{220,"homepna"},
{221,"gfp"},
{222,"ciscoISLvlan"},
{223,"actelisMetaLOOP"},
{224,"fcipLink"},
{225,"rpr"},
{226,"qam"},
{227,"lmp"},
{228,"cblVectaStar"},
{229,"docsCableMCmtsDownstream"},
{230,"adsl2"},
{231,"macSecControlledIF"},
{232,"macSecUncontrolledIF"},
{233,"aviciOpticalEther"},
{234,"atmbond"},
{235,"voiceFGDOS"},
{236,"mocaVersion1"},
{237,"ieee80216WMAN"},
{238,"adsl2plus"},
{239,"dvbRcsMacLayer"},
{240,"dvbTdm"},
{241,"dvbRcsTdma"},
{242,"x86Laps"},
{243,"wwanPP"},
{244,"wwanPP2"},
{245,"voiceEBS"},
{246,"ifPwType"},
{247,"ilan"},
{248,"pip"},
{249,"aluELP"},
{250,"gpon"},
{251,"vdsl2"},
{252,"capwapDot11Profile"},
{253,"capwapDot11Bss"},
{254,"capwapWtpVirtualRadio"},
{255,"bits"},
{256,"docsCableUpstreamRfPort"},
{257,"cableDownstreamRfPort"},
{258,"vmwareVirtualNic"},
{259,"ieee802154"},
{260,"otnOdu"},
{261,"otnOtu"},
{262,"ifVfiType"},
{263,"g9981"},
{264,"g9982"},
{265,"g9983"},
{266,"aluEpon"},
{267,"aluEponOnu"},
{268,"aluEponPhysicalUni"},
{269,"aluEponLogicalLink"},
{270,"aluGponOnu"},
{271,"aluGponPhysicalUni"},
{272,"vmwareNicTeam"},
{277,"docsOfdmDownstream "},
{278,"docsOfdmaUpstream"},
{279,"gfast"},
{280,"sdci"},
{281,"xboxWireless"},
{282,"fastdsl"},
{283,"docsCableScte55d1FwdOob"},
{284,"docsCableScte55d1RetOob"},
{285,"docsCableScte55d2DsOob"},
{286,"docsCableScte55d2UsOob"},
{287,"docsCableNdf"},
{288,"docsCableNdr"},
{289,"ptm"},
{290,"ghn"},
{-1,"End"},
};
void GetifTypeInfo(int ifType,char*ifTypeDescr){
	int i=0;
	while(ifTypeInfoList[i].ifType>0){
		if(ifTypeInfoList[i].ifType==ifType){
			strcpy(ifTypeDescr,ifTypeInfoList[i].Desrc);
			return;
		}
		i++;
	}
	strcpy(ifTypeDescr,"UnKnow");
}
